package edu.guigu.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyService2 extends Service {

    private MyBinder myBinder;
    public MyService2() {

    }

    // 首次创建服务时，系统将调用此方法来执行一次性设置程序
    // 如果服务已在运行，则不会调用此方法。该方法只被调用一次
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("TAG", getClass().getSimpleName() + " onCreate: ");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("TAG", getClass().getSimpleName() + " onBind: ");
        if(myBinder == null){
            myBinder = new MyBinder();
        }
        return myBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("TAG", getClass().getSimpleName() + " onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("TAG", getClass().getSimpleName() + " onDestroy: ");
        myBinder = null;
    }

    class MyBinder extends Binder {
        //  封装了一个用来获取到服务对象的方法
        public MyService2 getService(){
            return MyService2.this;
        }

        public void startMusice(){
            Log.d("TAG", "startMusice: ~~~~~");
        }
        public void pauseMusice(){
            Log.d("TAG", "pauseMusice: ~~~~~");
        }
    }
}
